package com.devshephard.thread;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@Component
public class AsyncServiceImpl implements AsyncService{

    @Async("asyncServiceExecutor")
    public void executeAsync(List<String> logOutputResults, CountDownLatch countDownLatch) {
        try{
            System.out.println("start executeAsync");

            //异步线程要做的事情
            for (String logOutputResult : logOutputResults) {
                System.out.println(Thread.currentThread().getName() + logOutputResult);
                Thread.sleep(100);
            }

            System.out.println("end executeAsync");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            countDownLatch.countDown();// 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放
        }
    }

}
